from sqlalchemy import Boolean, Index, String, DateTime, Date, Integer, DECIMAL, Column, UniqueConstraint
from config.database import Base
from sqlalchemy.orm import declared_attr

class BollBase(Base):
    """
    股票k线boll表
    """
    __abstract__ = True
    @declared_attr.directive
    def __table_args__(cls):
        return (
            UniqueConstraint("symbol", "trade_date", "data_type","lower_type","upper_type",
                             name=f"{cls.__tablename__}_symbol_tradedate_xtypes"),
            Index(f'{cls.__tablename__}_symbol', cls.symbol),
            Index(f'{cls.__tablename__}_trade_date', cls.trade_date),
            Index(f'{cls.__tablename__}_data_type', cls.data_type),
            Index(f'{cls.__tablename__}_lower_type', cls.lower_type),
            Index(f'{cls.__tablename__}_upper_type', cls.upper_type)
        )
    id = Column(Integer, primary_key=True, autoincrement=True, nullable=False, comment='id')
    trade_date = Column(Date, nullable=False, comment='交易日期')
    symbol = Column(String(20), nullable=False, comment='股票代码')
    open = Column(DECIMAL, nullable=False, comment='开盘价')
    close = Column(DECIMAL, nullable=False, comment='收盘价')
    low = Column(DECIMAL, nullable=False, comment='最低价')
    high = Column(DECIMAL, nullable=False, comment='最高价')
    upper = Column(DECIMAL, nullable=True, comment='上轨')
    middle = Column(DECIMAL, nullable=True, comment='中轨')
    lower = Column(DECIMAL, nullable=True, comment='下轨')
    data_type = Column(String(10), nullable=True, comment='计算的数据类型')
    lower_type = Column(String(10), nullable=True, comment='比较低值的数据类型')
    upper_type = Column(String(10), nullable=True, comment='比较高值的数据类型')
    bottom_divergence = Column(Boolean, nullable=True, comment='底背离信号')
    top_divergence = Column(Boolean, nullable=True, comment='顶背离信号')
    create_time = Column(DateTime, nullable=False, comment='创建时间')
    update_time = Column(DateTime, nullable=False, comment='更新时间')